package com.spi.soconversion.orders;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;

import com.spi.soconversion.base.SOCException;
import com.spi.soconversion.util.DBConnection;
import com.spi.soconversion.util.ErrorConstants;
import com.spi.soconversion.util.ErrorMessageReader;
import com.spi.soconversion.util.QueryConstants;
import com.spi.soconversion.util.QueryReader;
import com.spi.soconversion.util.SOCConstants;
import com.spi.soconversion.util.SOCFileWriter;
import com.spi.soconversion.util.SOCUtility;


public class SOCChargeNames {
	private static final String SKU = "SKU";
	private static final String NAME = "NAME";
	private static final String GROUP1 = "GROUP1";
	private static HashMap<String,SOCCharge> headerChargesMap = new HashMap<String,SOCCharge>();
	private static HashMap<String,SOCCharge> lineChargesMap = new HashMap<String,SOCCharge>();
	private static HashMap<String,SOCCharge> discountChargesMap = new HashMap<String,SOCCharge>();
	private static HashMap<String,SOCCharge> returnChargesMap = new HashMap<String,SOCCharge>();
	private static HashMap<String,SOCCharge> emptyChargeCategoryMap = new HashMap<String,SOCCharge>();
	private String d_logFileName = null;
	private SOCFileWriter d_fileWriter = null;
	
	public SOCChargeNames(String logFileName, SOCFileWriter fileWriter)
	{
		d_logFileName = logFileName;
		d_fileWriter = fileWriter;
	}
	
	public void populateChargeNames() throws SOCException
	{
		String chargeNamesQuery = QueryReader.getQuery(QueryConstants.GET_CHARGE_NAMES_QUERY);
        	
		Statement stmt=null;
    	ResultSet rsInfo=null;
    	d_fileWriter.write(d_logFileName, SOCUtility.writeLogMessage(SOCConstants.MESSAGE_FETCH_CHARGE_NAMES_STARTS));
    	try
		{
    		stmt=DBConnection.getConnection().createStatement();
    		rsInfo=stmt.executeQuery(chargeNamesQuery);
    		SOCCharge charge = null;
			if(rsInfo!=null)
			{
				while(rsInfo.next())
				{
					charge = new SOCCharge();
					String chargeCategory = rsInfo.getString(GROUP1);
					String chargeName = rsInfo.getString(NAME);
					String chargeSKU = rsInfo.getString(SKU);
					charge.setChargeName(chargeName);
					charge.setChargeSKU(chargeSKU);
					if(SOCConstants.ALLOWANCE.equals(chargeCategory))
					{
						headerChargesMap.put(chargeSKU,charge);
					}
					else if(SOCConstants.CHARGE.equals(chargeCategory))						
					{
						lineChargesMap.put(chargeSKU,charge);
					}
					else if(SOCConstants.DISCOUNT.equals(chargeCategory))						
					{
						discountChargesMap.put(chargeSKU,charge);
					}
					else if(SOCConstants.RETURN.equals(chargeCategory))						
					{
						returnChargesMap.put(chargeSKU,charge);
					}
					else
					{
						emptyChargeCategoryMap.put(chargeSKU,charge);
					}
				}
				
			}			
		}
		catch(SQLException sqle)
		{
			d_fileWriter.write(d_logFileName, SOCUtility.writeLogMessage(ErrorMessageReader.getError(
    				ErrorConstants.FAILED_FETCH_CHARGE_NAMES)+SOCConstants.NEW_LINE+sqle.getMessage()));
			throw new SOCException(
            		ErrorMessageReader.getError(
            				ErrorConstants.FAILED_FETCH_CHARGE_NAMES) ,sqle);
    		
		}	
		d_fileWriter.write(d_logFileName, SOCUtility.writeLogMessage(SOCConstants.MESSAGE_FETCH_CHARGE_NAMES_ENDS));
	}
	
	public static HashMap<String,SOCCharge> getHeaderChargeMap()
	{
		return headerChargesMap;
	}
	
	public static HashMap<String,SOCCharge> getLineChargeMap()
	{
		return lineChargesMap;
	}
	
	public static String getSVGChargeName(String sku)
	{
		String result=SOCConstants.EMPTY_STRING;
		if(sku!=null)
		{
			SOCCharge charge = emptyChargeCategoryMap.get(sku.replaceAll("^0*",""));
			if(charge!=null)
			{
				result = charge.getChargeName();
			}
		}
		return result;
	}
}
